//
//  NetProgressView.m
//
//  Created by SKY
//  Copyright © 翊sky. All rights reserved.
//

#import "NetProgressView.h"

#define PROGRESS_SUPER_WIDTH    64
#define PROGRESS_SUPER_HEIGHT   64

#define PROGRESS_SIGN_WIDTH     20
#define PROGRESS_SIGN_HEIGHT    20

#define PROGRESS_SUB_WIDTH      20
#define PROGRESS_SUB_HEIGHT     20

#define PROGRESS_CIRCLR_RADIUS  20

@interface NetProgressView ()

@property (nonatomic,strong) UIView *signView;

@end

static CGFloat endAngle = -(M_PI_2 + M_PI_4);

@implementation NetProgressView

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 255 / 256.0, 222 / 256.0, 173 / 256.0, 0.8);
    CGContextSetLineWidth(context, 1.5);
    CGContextSetLineJoin(context, kCGLineJoinMiter);
    
    CGContextAddEllipseInRect(context, CGRectMake((PROGRESS_SUPER_WIDTH - PROGRESS_SUB_HEIGHT) * 0.5, (PROGRESS_SUPER_HEIGHT - PROGRESS_SUB_HEIGHT) * 0.5, PROGRESS_SUB_WIDTH, PROGRESS_SUB_WIDTH));
    CGContextDrawPath(context, kCGPathStroke);

    CGContextAddArc(context, PROGRESS_SUPER_WIDTH * 0.5, PROGRESS_SUPER_HEIGHT * 0.5, PROGRESS_CIRCLR_RADIUS, -(M_PI_2 + M_PI_4), endAngle, 0);
    CGContextDrawPath(context, kCGPathStroke);
    
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    
    self.frame = CGRectMake(0, 0, PROGRESS_SUPER_WIDTH, PROGRESS_SUPER_HEIGHT);
    
    self.center = newSuperview.center;
    
    self.transform = CGAffineTransformMakeRotation(M_PI_4);
    [self addShadowAtView:self radius:5 shadowOffset:CGSizeMake(5, 5)];
    
    [self createSignView:newSuperview];
    
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(circle) userInfo:nil repeats:YES];
    
}

- (void)circle {
    
    if (endAngle >= (360 - 90) * M_PI_2 / 90) {
        endAngle = -(M_PI_2 + M_PI_4);
    }
    endAngle += M_PI_2 / 90 * 20;
    [self setNeedsDisplay];
}

- (void)createSignView:(UIView *)superview {
    
    self.signView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, PROGRESS_SIGN_WIDTH, PROGRESS_SUB_HEIGHT)];
    [superview addSubview:self.signView];
    
    [self addShadowAtView:self.signView radius:3 shadowOffset:CGSizeMake(3, 3)];
    self.signView.transform = CGAffineTransformMakeRotation(M_PI_4);
    
    CGPoint center = superview.center;
    center.y = center.y + PROGRESS_SUPER_HEIGHT * 0.5 + 30;;
    self.signView.center = center;
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.translation.y";
    CAMediaTimingFunction *tf = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    animation.timingFunction = tf;
    animation.repeatCount = MAXFLOAT;
    animation.values = @[@50,@0,@20];
    [self.signView.layer addAnimation:animation forKey:nil];
}

-(void)removeFromSuperview {
    
    [self.signView.layer removeAllAnimations];
    [self.signView removeFromSuperview];
    self.signView = nil;
    
    [super removeFromSuperview];
}

- (void)addShadowAtView:(UIView *)view radius:(CGFloat)radius shadowOffset:(CGSize)set {
    
    view.backgroundColor = [UIColor whiteColor];
    view.layer.shadowColor = [UIColor blackColor].CGColor;
    view.layer.shadowOffset = set;
    view.layer.shadowOpacity = 0.8;
    view.layer.shadowRadius = radius;
}

@end
